import { addCardAPI, deleteCardAPI, editCardAPI, fetchCardListAPI, fetchProportionAPI, findCardAPI, renewCardAPI } from '@/api/car/monthCard'

const actions = {
  /* 发送请求获取月卡列表的action */
  async fetchCardList(context, pageParams) {
    const { data: cardListInfo } = await fetchCardListAPI(pageParams)
    context.commit('updateCardList', cardListInfo)
  },
  /* 发送请求删除月卡的action */
  async deleteCard(context, id) {
    const { msg } = await deleteCardAPI(id)
    return msg
  },
  /* 发送请求添加月卡的action */
  async addCard(context, formData) {
    const { msg } = await addCardAPI(formData)
    return msg
  },
  /* 发送请求查找月卡的action */
  async findCardAPI(context, id) {
    const { data } = await findCardAPI(id)
    return data
  },
  /* 发送请求编辑月卡的action */
  async editCard(context, formData) {
    const { msg } = await editCardAPI(formData)
    return msg
  },
  /* 发送请求续费月卡的action */
  async renewCard(context, formData) {
    const { msg } = await renewCardAPI(formData)
    return msg
  },
  /* 发送请求获取月卡占有率的action */
  async fetchProportion(context) {
    const { data } = await fetchProportionAPI()
    context.commit('updateProportion', data)
  }
}
const mutations = {
  /* 更新获取月卡列表的mutation */
  updateCardList(state, cardListInfo) {
    state.cardListInfo = cardListInfo
  },

  /* 更新月卡占有率的mutation */
  updateProportion(state, proportion) {
    state.proportion = proportion
  }
}
const state = () => {
  return {
    cardListInfo: {},
    proportion: {}
  }
}

const getters = {
  cardList(state) {
    return state.cardListInfo.rows
  },
  total(state) {
    return state.cardListInfo.total
  }
}

export default {
  actions,
  mutations,
  state,
  getters,
  namespaced: true
}
